package com.hs.utils;

import com.alibaba.fastjson.JSON;
import com.hs.common.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


public class ResultUtils<T> {

    public static void success(HttpServletResponse response,List list,String msg) throws IOException {
        PrintWriter out = response.getWriter();
        Result result = new Result();
        result.setCode(0);
        result.setMsg(msg);
        result.setCount(list.size());
        result.setData(list);
        out.print(JSON.toJSONString(result));
    }
    public static void success(HttpServletResponse response,Object obj ,String msg) throws IOException {
        PrintWriter out = response.getWriter();
        Result result = new Result();
        result.setCode(0);
        result.setMsg(msg);
        result.setData(obj);
        out.print(JSON.toJSONString(result));
    }

    public static void success(HttpServletResponse response,Integer forgetId,String msg) throws IOException {
        PrintWriter out = response.getWriter();
        Result result = new Result();
        result.setCode(0);
        result.setMsg(msg);
        result.setForgetId(forgetId);
        out.print(JSON.toJSONString(result));
    }

    public static void success(HttpServletResponse response,String msg) throws IOException {
        PrintWriter out = response.getWriter();
        Result result = new Result();
        result.setCode(0);
        result.setMsg(msg);
        out.print(JSON.toJSONString(result));
    }

    public static void error(HttpServletResponse response,Integer code,String msg) throws IOException {
        PrintWriter out = response.getWriter();
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        out.print(JSON.toJSONString(result));
    }

    public static void jsonString(HttpServletResponse response,Object obj) throws IOException {
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(obj));
    }
}
